#!/bin/bash
########################################################################
# script name:  tl10_upgrade.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/03    0.1     MDUMONT         initial
# 2011/02/09    0.2     MDUMONT         addept status file
# 2011/02/14    0.3     MDUMONT         dev test + further improvement on logging
# 2011/02/17    0.4     MDUMONT		changed some log files
########################################################################
#######################################
# Variables and environnement
#######################################
LOGDIR=$HOME
OSLEVELBEFORE=${LOGDIR}/oslevel.before.txt
OSLEVELAFTER=${LOGDIR}/oslevel.after.txt
BOOTLISTLOGAFTER=$LOGDIR/bootlist_normal_after.txt
BOOTLISTLOGB4=$LOGDIR/bootlist_normal_b4.txt
PACKLOG=$LOGDIR/packlog.txt
OSWANTED="5300-10-04-1015"
DFKAFTER=${LOGDIR}/dfk_after.txt
DFKBEFORE=${LOGDIR}/dfk_before.txt
###################################################################
# MKSYSB file is taken from the /drMntPt/script/DrMksysb.ksh script
###################################################################
MKSYSBFILE=/drMntPt/`uname -n`_`date +'%d%b%y'`.mksysb
#######################################
# FUNCTIONS
#######################################
function echo_status {
	DATE=$(date +'%Y-%m-%d-%H-%M-%S')
	#############################
	# $1 = step level
	# $2 = status
	# $3 = comment
	#############################
	STATUS="$DATE:$1:$2:$3"
	echo $STATUS | tee -a $TL10PROGRESS
}
function mountdir {
	if [ ! -d $2 ]; then mkdir $2; fi
	echo "Trying to mount $1 on $2"
	DIR=$(echo $1 | cut -d: -f2)
	mount | grep $DIR | grep -q $2
	RC=$?
	if [ $RC == 0 ]
	then
		echo "Mount $1 already mounted on $2"
	else
		mount $1 $2
		RC=$?
		if [ $RC != 0 ]; then echo "cannot mount $1 on $2" ; exit 1; fi
	fi
}
function exec_STEP1 {
	echo_status 1 start mksysb_creation
	echo -n "Do you want to create a mksysb? [Y/n] "
	read ANSWER
	if [ "$ANSWER" == "Y" ]
	then
		mountdir mtlbcon1:/aixcdrom/inhouse/aix5.3tl10sp4 /tmp/mnt
		mountdir mtlbcon1:/aixcdrom/inhouse/ML2_all_cd /tmp/mnt2
		mountdir mtlbcon1:/aixNetInst/dr /drMntPt/

		echo "Executing /drMntPt/script/DrMksysb.ksh -m... "
		/drMntPt/script/DrMksysb.ksh -m
		mountdir mtlbcon1:/aixNetInst/dr /drMntPt/
		echo "Verifying mksysb $MKSYSBFILE"
		if [ ! -f $MKSYSBFILE ]
		then
			echo "--> File $MKSYSBFILE doesn't exist"
			echo "--> Please verify and re-execute $0"
			rm $TL10PROGRESS
			exit 1
		fi
		lsmksysb -lf $MKSYSBFILE
		RC=$?; echo Exit code is $RC
		if [ $RC != 0 ]
		then
			echo "--> mksysb doesn't look good, please verify before re-executing $0"
			rm $TL10PROGRESS
		else
			echo "-->"
			echo "--> mksysb $MKSYSBFILE ok, continue..."
			echo "-->"
		fi
	fi
	bootinfo -b > /tmp/bootinfo.txt
	bosboot -ad /dev/hdisk0 > /tmp/bosboot.txt
	bosboot -ad /dev/ipldevice >> /tmp/bosboot.txt
	bootlist -om normal > /tmp/bootlist.txt
	echo_status 1 finish mksysb_done
	exit 0
}
function exec_STEP2 {
	echo_status 2 start verifying_packages_and_bootlist
	oslevel -s > $OSLEVELBEFORE
	(
	echo "-->"
	echo "--> installp -s"
	echo "-->"
	installp -s
	echo "-->"
	echo "--> installp -cgX"
	echo "-->"
	installp -cgX all
	echo "-->"
	echo "--> installp -s"
	echo "-->"
	installp -s
	) | tee $PACKLOG
	lppchk -v -m3
	(
	echo "#####################"
	echo "### bootlist before #"
	echo "#####################"
	bootlist -om normal
	) | tee $BOOTLISTLOGB4
	df -k > $DFKBEFORE
	echo "--> Preventive shutdown and reboot: execute shutdown -Fr"
	echo_status 2 finish
	exit 0
}
function exec_STEP3 {
	echo_status 3 start UPGRADE

	mountdir mtlbcon1:/aixcdrom/inhouse/aix5.3tl10sp4 /tmp/mnt
	mountdir mtlbcon1:/aixcdrom/inhouse/ML2_all_cd /tmp/mnt2

	### smit install_all ###
	echo "Checking for X11.samples.apps.clients..."
	/usr/lib/instl/sm_inst installp_cmd -a -l -d /tmp/mnt2 -f X11.samples.apps.clients -c -N -g -X -Y
	RC=$?; echo Exit code is $RC

	echo
	echo -n "Looks ok [Y/n] "
	read ANSWER
	if [ "$ANSWER" == "Y" ]
	then
		echo "Checking for Java5.sdk..."
	        /usr/lib/instl/sm_inst installp_cmd -a -l -d /tmp/mnt -f Java5.sdk -c -N -g -X -Y
		RC=$?; echo Exit code is $RC
	else
		echo "You have not answered Y, exiting"
		exit 1
	fi

	lslpp -L | grep rsct.basic
	### PREVIEW ONLY ###
	/usr/lib/instl/sm_inst installp_cmd -a -d /tmp/mnt -f _update_all -p -c -N -g -X -Y | tee /tmp/update_tl10.log
	echo
	echo "Starting the actual UPGRADE"
	echo -n "Do you want to continue? [Y/n] "
	read ANSWER
	if [ "$ANSWER" == "Y" ]
	then
		echo
	        /usr/lib/instl/sm_inst installp_cmd -a -d /tmp/mnt -f _update_all -c -N -g -X -Y | tee /tmp/update_tl10.log
		RC=$?; echo Exit code is $RC
	else
       		echo "You have not answered Y, exiting"
		exit 1
	fi

	echo
	echo -n "Looks ok [Y/n] "
	read ANSWER
	if [ "$ANSWER" != "Y" ]
	then
		echo "--> Not ok...exiting"
		exit 1
	fi
	cp -p /etc/inittab /etc/inittab_prev_b4tl10
	echo "other:2:once:/usr/bin/chmod o+w /var/adm/ras/conslog" >> /etc/inittab
	cat $BOOTLISTLOGB4
	(
	echo "####################"
	echo "### bootlist after #"
	echo "####################"
	bootlist -om normal
	) | tee $BOOTLISTLOGAFTER
	echo
	cat $OSLEVELBEFORE
	(
	echo "####################"
	echo "### OSLEVEL after  #"
	echo "####################"
	oslevel -s
	) | tee $OSLEVELAFTER
	echo -n "Looks ok [Y/n] "
	read ANSWER
	if [ "$ANSWER" == "Y" ]
	then
		echo "--> shutdown and reboot: execute shutdown -Fr"
	fi
	echo_status 3 finish
	exit 0
}
function exec_STEP4 {
	echo_status 4 start swap_and_tmp_size
	oslevel -s > /tmp/oslevel.after.txt
	lppchk -v -m3
	TOTALSWAP=$(lsps -a | grep -v Page | sed 's/MB//' | awk '{print $4}' | awk '{ swap += $1 } END { print swap }')
	WANTED=12000
	echo "SWAP is at $TOTALSWAP MB"
	if [ $TOTALSWAP -lt $WANTED ]
	then
		echo "SWAP is less then $WANTED MB"
		echo
		echo -n "Do you want to add swap? [Y/n]  "
		read ANSWER
		if [ "$ANSWER" == "Y" ]
		then
			PPSIZE=$(lsvg rootvg | grep 'PP SIZE' | awk '{print $(NF-1)}')
			DEVICE=$(lsps -a | grep -v Page | awk '{print $1}')
			ADDSPACE=$(echo "($WANTED - $TOTALSWAP)/$PPSIZE" | bc)
			echo "Verify the selected device is ok and execute the command --> chps -s $ADDSPACE $DEVICE <--"
		fi
	else
		echo "SWAP is higher then the wanted size of $WANTED MB"
	fi
	echo_status 4 finish
	exit 0
}
function exec_STEP5 {
	echo_status 5 start param_adjust
	##########################################
	# verify if /tmp is more then 2G ( K * 2 )
	##########################################
	TMPSIZE=$(echo "`df -k /tmp | grep -v Filesystem | awk '{print $2}'` * 2" | bc)
	################################
	# WANTED SIZE is in blocks ( K * 2 )
	# 2G = 4194304 blocks
	################################
	WANTEDTMPSIZE=4194304
	################################
	if [ $TMPSIZE -lt $WANTEDTMPSIZE ]
	then
		echo "TMP is at $TMPSIZE blocks"
		echo "Adjusting TMP to wanted size $WANTEDTMPSIZE"
		echo "-->"
		echo "--> chfs -a size=$WANTEDTMPSIZE /tmp"
		echo "-->"
	else
		echo "TMP (/tmp) size of $TMPSIZE blocks is more then the wanted $WANTEDTMPSIZE blocks"
	fi
	MAXUPROC=$(lsattr -El sys0 -a maxuproc | cut -d' ' -f2)
	WANTEDMAXUPROC=16384
	if [ $MAXUPROC -lt $WANTEDMAXUPROC ]
	then
		echo "MAXUPROC is at $MAXUPROC"
		echo "Adjusting MAXUPROC to $WANTEDMAXUPROC"
		echo "-->"
		echo "--> chdev -l sys0 -a maxuproc=$WANTEDMAXUPROC"
		echo "-->"
	else
		echo "MAXUPROC of $MAXUPROC is more then wanted $WANTEDMAXUPROC"
	fi
	NCARGS=$(lsattr -El sys0 -a ncargs | cut -d' ' -f2)
	WANTEDNCARGS=128
	if [ $NCARGS -lt $WANTEDNCARGS ]
	then
		echo "NCARGS is at $NCARGS"
		echo "Adjusting NCARGS to $WANTEDNCARGS"
		echo "--> chdev -l sys0 -a ncargs=128"
	else
		echo "NCARGS of $NCARGS is more then wanted $WANTEDNCARGS"
	fi
	df -k > $DFKAFTER
	diff $DFKBEFORE $DFKAFTER
	oslevel -s > OSLEVELAFTER
	echo
	echo "--> OSLEVEL before: `cat $OSLEVELBEFORE`"
	echo "--> OSLEVEL now: `cat $OSLEVELAFTER`"
	echo "--> Restart apps"
	echo_status 5 finish
	exit 0
}
################################################################################
# MAIN
################################################################################
TL10PROGRESS=/tmp/tl10_progress_status
NUMSTEPS=5
if [ ! -f $TL10PROGRESS ]
then 
	echo "Verifying oslevel..."
	OSLEVEL=$(oslevel -s)
	#########################
	# put != in prod script
	#########################
	if [ "$OSLEVEL" != "$OSWANTED" ]
	then
		echo -n "Your AIX version $OSLEVEL isn't $OSWANTED, do you want to upgrade? [Y/n] "
		read ANSWER
	else
		echo -n "Already at ${OSWANTED}, still want to upgrade? [Y/n] "
		read ANSWER
	fi
	if [ "$ANSWER" == "Y" ]
	then
		echo_status 0 Begin
		PRESENTSTEP=1
	else
		echo "Exiting..."
		exit 1
	fi
else
	LASTLINE=$(tail -1 $TL10PROGRESS)
	LASTSTEPS=$(echo $LASTLINE | cut -d: -f2)
	LASTSTATUS=$(echo $LASTLINE | cut -d: -f3)
	echo "File $TL10PROGRESS exist."
	echo "The last executed step was ${LASTSTEPS}/${NUMSTEPS} and the status was $LASTSTATUS"
	if [ "$LASTSTATUS" != "finish" ]
	then
		echo "Last step $LASTSTEPS didn't complete successfully, please verify."
		exit 1
	else
		######################################
		# finished ok so move on to other step
		######################################
		if [ $LASTSTEPS == $NUMSTEPS ]; then echo "Last step ($NUMSTEPS) already done, exiting..."; fi
		PRESENTSTEP=$(( LASTSTEPS + 1))
	fi
fi
while [ $PRESENTSTEP -le $NUMSTEPS ]
do
	exec_STEP${PRESENTSTEP}
	((PRESENTSTEP++))
done
################################################################################
exit 0
